package cn.webrx;

import cn.webrx.beans.Result;
import cn.webrx.mapper.DbMapper;
import cn.webrx.pojo.User;
import cn.webrx.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@SpringBootApplication
@RestController
public class App {


    @Resource
    DbMapper dm;

    @Resource
    UserService us;


    @PostMapping("/addUser")
    public Map<String, Object> addUser(@RequestBody User user) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 400);
        map.put("msg", "添加用户失败");
        try {
            if (us.save(user)) {
                map.put("code", 200);
                map.put("msg", "添加成功");
            }
        } catch (Exception e) {
            map.put("msg", "账号已经存在保存失败了");
        }

        return map;
    }

    @GetMapping("/dbs")
    public Set<String> dbs() {
        return dm.dbs();
    }

    @GetMapping("/list")
    public Result list() {
        Result result = new Result();
        result.setCode(200);
        result.setMsg("查询成功");
        result.setData(us.list());
        return result;
    }

    @DeleteMapping("/del/{id}") // ('/del/100',{})
    public Result deleteById(@PathVariable long id) {
        if (us.removeById(id)) {
            return Result.builder().code(200).msg("删除成功").build();
        } else {
            return Result.builder().code(400).msg("删除失败").build();
        }
    }


    @DeleteMapping("/remove")  // ('/del',{params:{id:1000}})
    public Result remove(long id) {
        if (us.removeById(id)) {
            return Result.builder().code(200).msg("删除成功").build();
        } else {
            return Result.builder().code(400).msg("删除失败").build();
        }
    }

    @DeleteMapping("/delall")  // ('/delall?ids=1&ids=2&ids=3')
    public Result delall(@RequestParam List<Long> ids) {
        //us.removeBatchByIds(ids)
        if (us.removeByIds(ids)) {
            return Result.builder().code(200).msg("删除成功" + ids.size() + "条记录").build();
        } else {
            return Result.builder().code(400).msg("删除失败").build();
        }
    }

    @PutMapping("/update")
    public Result update(@RequestBody User user) {
        if (us.updateById(user)) {
            return Result.builder().code(200).msg("更新成功").build();
        } else {
            return Result.builder().code(400).msg("无法更新").build();
        }
    }


    @GetMapping("/pages")
    public Map<String, Object> pages(int currpage, int pagesize) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "分页查询成功");

        //mybatis 分页查询 ,先配置分页插件
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        IPage<User> page = us.page(new Page<>(currpage, pagesize), wrapper);
        System.out.println(page.getCurrent());//当前页
        System.out.println(page.getSize());//每页多少条
        System.out.println(page.getPages());//总页数
        System.out.println(page.getTotal());//总记录数
        System.out.println(page.getRecords());//当前页的数据记录
        map.put("total", page.getTotal());
        map.put("data", page.getRecords());
        return map;
    }


    @GetMapping("/users")
    public cn.webrx.beans.User getUser() {
        return new cn.webrx.beans.User(199, "李四六");
    }


    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

}
